package com.shopping.cart.domain

import grails.test.GrailsUnitTestCase
import org.apache.commons.lang.RandomStringUtils
import com.shopping.cart.Item
import com.shopping.cart.ItemCategory

class ItemTests extends GrailsUnitTestCase{

    void testThatConstraintsActuallyWork() {
        mockDomain Item
        Item item = new Item()

        item.description = RandomStringUtils.randomAlphanumeric(513)
        assertEquals false, item.validate()
        assertEquals 4, item.errors.errorCount

        assertEquals 'maxSize', item.errors.description
        assertEquals 'minSize', item.errors.categories
        assertEquals 'nullable', item.errors.title
        assertEquals 'nullable', item.errors.amount
    }

    void testSaveWorks(){
        mockDomain Item
        mockDomain ItemCategory
        Item item = new Item(title:'yo', amount: 1.1)
        item.addToCategories new ItemCategory()

        assertTrue item.validate()
    }
}